package com.yzt.message.custom;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yzt.message.R;
import com.yzt.message.widget.chatinput.utils.TextSpanUtils;

import java.util.List;
import java.util.Map;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.MessageContent;

public class RXShowTipMessageItemProvider extends CustomBaseMessageItemProvider<RXShowTipMessageContent> {

    public RXShowTipMessageItemProvider() {
        mConfig.showReadState = false;
        mConfig.showPortrait = false;
        mConfig.showContentBubble = false;
        mConfig.centerInHorizontal = true;
    }

    @Override
    protected io.rong.imkit.widget.adapter.ViewHolder onCreateMessageContentViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.nomi_rc_item_show_tip_text_message, viewGroup, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder, io.rong.imkit.widget.adapter.ViewHolder viewHolder1, RXShowTipMessageContent content, UiMessage data, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        ViewHolder holder = (ViewHolder) viewHolder;
        String contentStr = content.getContent();
        holder.message.setText(contentStr);

        Map<String, Object> extra = content.getMyExtra();
        if (extra != null && extra.size() > 0) {

            if (extra.containsKey("highlightText")) {
                String highlightText = (String) extra.get("highlightText");

                int start = contentStr.indexOf(highlightText);
                SpannableString spannableString =new SpannableString(content.getContent());
                if (TextSpanUtils.isCanSetSpan(start, start+highlightText.length())){
                    spannableString.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                        }


                        @Override
                        public void updateDrawState(TextPaint ds) {
                            super.updateDrawState(ds);
                            ds.setColor(Color.parseColor("#589CFF"));
                            ds.setUnderlineText(false);      //设置下划线

                        }

                    },start,start+highlightText.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

                holder.message.setHighlightColor(holder.message.getContext().getResources().getColor(android.R.color.transparent));
                holder.message.setText(spannableString);
                holder.message.setMovementMethod(LinkMovementMethod.getInstance());
            }
        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXShowTipMessageContent rxShowTipMessageContent, UiMessage uiMessage, int i, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXShowTipMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXShowTipMessageContent data) {
        if (data == null)
            return null;
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }
        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder{
        TextView message;

        public ViewHolder(Context context, View view) {
            super(context, view);

            this.message = view.findViewById(R.id.tv_content);
        }
    }
}

